package JDBC_Test.book.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import JDBC_Test.book.JDBCUtil;
import JDBC_Test.book.Model.BookModel;

public class BookDao {
	Connection con = null;
	Statement st = null;
	PreparedStatement ps = null;

	/**
	 * 记录进书
	 * 
	 * @param list
	 * @throws SQLException
	 */
	public void insertList(List<BookModel> list) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "insert into book (book_no,book_name) values(?,?)";
		try {
			con = JDBCUtil.geConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement(sql);
			for (int i = 0; i < list.size(); i++) {
				String sqlquery = "select book_no from book where book_no = '" + list.get(i).getBookNo() + "'";
				PreparedStatement ps1 = con.prepareStatement(sqlquery);
				ResultSet rs = ps1.executeQuery();
				String bNo = null;
				while (rs.next()) {
					bNo = rs.getString("book_no");
				}
				if (list.get(i).getBookNo().equals(bNo))
					continue;
				BookModel bookModel = list.get(i);
				ps.setString(1, bookModel.getBookNo());
				ps.setString(2, bookModel.getBookName());
				ps.addBatch();
			}
			ps.executeBatch();
			ps.clearBatch();
			con.commit();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JDBCUtil.close(con, st, ps);
		}

	}

}
